package cn.nwpu.eckel.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.nwpu.eckel.model.Forecast;
import cn.nwpu.eckel.model.Order;
import cn.nwpu.eckel.model.ScheduleReceipt;
import cn.nwpu.eckel.model.Source;
import cn.nwpu.eckel.model.User;

/**
 * Servlet implementation class NewSource
 */
@WebServlet("/NewSource")
public class NewSource extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewSource() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Source theSource=null;
		int mpsNo=Integer.parseInt(request.getParameter("mpsNo"));
		String source=request.getParameter("source");
		int ts=Integer.parseInt(request.getParameter("ts"));
		int quantity=Integer.parseInt(request.getParameter("quantity"));
		HttpSession session=request.getSession();
		User user=(User)session.getAttribute("user");
		String people=user.getName();
		if(source.equals("order")){
			theSource=new Order(mpsNo,ts,quantity,people);
		}else if(source.equals("forecast")){
			theSource=new Forecast(mpsNo,ts,quantity,people);
		}else{
			theSource=new ScheduleReceipt(mpsNo,ts,quantity,people);
		}
		theSource.add();
		theSource.afterAdd();
		String rePath="index.jsp?mpsId="+mpsNo+"&source="+source;
		response.sendRedirect(rePath);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
